from flask_restful import Resource
from flask_restful.reqparse import RequestParser

from app.serve.content.article import ArticleServe
from app.serve.user.auth import login_required
from app.util.exception import WtreesReturn, ErrorType


class ContentCreate(Resource):
    """
     新建和修改文章
    """
    post_data = RequestParser()
    post_data.add_argument("id", type=int, help='')
    post_data.add_argument("title", type=str, help='标题')
    post_data.add_argument("abstract", type=str, help='摘要', required=True)
    post_data.add_argument("show_time", type=str, help='发布时间', required=False)
    post_data.add_argument("source", type=str, help='来源', required=False)
    post_data.add_argument("content", type=str, help='正文', required=False)
    post_data.add_argument("column_id", type=str, help='栏目ID', required=False)
    post_data.add_argument("nature_id", help='属性ID')

    @login_required
    def post(self):
        args = self.post_data.parse_args()
        ArticleServe.article_create(title=args['title'],
                                    abstract=args['abstract'],
                                    show_time=args['show_time'],
                                    source=args['source'],
                                    content=args['content'],
                                    column_id=args['column_id'],
                                    nature=args['nature_id']
                                    )
        return WtreesReturn.data(ErrorType.OK)

    @login_required
    def put(self):
        args = self.post_data.parse_args()
        ArticleServe.article_after(title=args['title'],
                                   abstract=args['abstract'],
                                   show_time=args['show_time'],
                                   source=args['source'],
                                   content=args['content'],
                                   column_id=args['column_id'],
                                   nature=args['nature_id']
                                   )
        return WtreesReturn.data(ErrorType.OK)


class ContentList(Resource):
    """
     文章列表
    """
    get_data = RequestParser()
    get_data.add_argument("search", type=str, help='标题')
    """
     文章详情
    """
    post_data = RequestParser()
    post_data.add_argument("id", type=int, help='标题')

    @login_required
    def get(self):
        args = self.get_data.parse_args()
        data, page = ArticleServe.article_list(search=args['search'])
        return WtreesReturn.data(ErrorType.OK, data=data, page=page)

    @login_required
    def post(self):
        args = self.post_data.parse_args()
        data = ArticleServe.article_detail(id=args['id'])
        return WtreesReturn.data(ErrorType.OK, data=data)
